<?php
namespace Home\Controller;
use Think\Controller;
class NewsController extends \Ac {
	
	public $isAdmin=false;
	public $login_expire = 3600;
	public function __construct(){
		parent::__construct();
		$this->maxPic = 1;
	}
	
    public function lists(){
		$this->model = "News";
		$orderSQL = $this->order();		
		$M = M($this->model);
		$M->table(_PRE_.'news as P');
		$M->join(_PRE_.'news_cate as C on P.cid=C.cid');
		if($this->fc['act'] == "search"){//搜索
			$whereSQL = "1=1";
			if($this->fc['title']){
				$whereSQL .= " and P.title like '%".$this->fc['title']."%'";
				$this->pname = $this->fc['pname'];
			}
			if($this->fc['cid']){
				$whereSQL .= " and C.cid = '".$this->fc['cid']."'";
				$pid = $this->cid = $this->fc['cid'];
			}		
			if($this->fc['order']){
				$orderSQL = $this->order($this->fc['order']);
				$this->order = $this->fc['order'];
			}
			$M->where($whereSQL);
		}			
		$MCount = clone $M;
		$MyPage = MyPage($MCount->count(), 10);
		$M->limit($MyPage['firstRow'] . ',' . $MyPage['listRows']);
		$M->field('P.*,C.cname');
		$M->order($orderSQL);
		$list = $M->select();	
		
		$cate = cateFun("News_cate",$pid);//分类
		$this->cate = $cate;
						
        $this->assign("page", $MyPage['page']);
        $this->assign("lists", $list); //数据循环变量
		$this->assign('empty','<tr><td colspan="6" align="center">没有找到数据</td></tr>');
		$this->assign('tabs',"文章管理");//功能名称
		$this->display("template/news/lists");
    }
	
	public function edit(){
		$this->model = "News";
		$this->assign('act',"add");//操作方法
		$this->assign('tabs',"新增文章");//功能名称
		$navi = $this->navigation(11);
		if($_GET['act']=="edit"){
			$this->assign('act',"edit");//操作方法	
			$res = M($this->model)->where('id='.$_GET['id'])->find();
			if(!empty($res['pic'])){
				$res['ppic'] = $res['pic'];
				$res['pic'] = explode(',',$res['pic']);
			}
			$this->assign('tabs',"修改文章");//功能名称
			$navi = $this->navigation(12);
		}
		$this->html = MyHtml("ncontent",$res['ncontent']);
		$this->cate = cateFun("News_cate",$res['cid']);//分类
		$this->res = $res;
		$this->navig = $navi['nav'];
		$this->lasturl = $navi['lasturl'];
		$this->callback = $this->lasturl;//回调地址
		$this->display("template/news/edit");
	}
	
	/*
	*	分类
	*/
	public function cate(){	
		$this->model = "News_cate";
		$arr = array();
		$Cate = M($this->model);
		$list = $Cate->order('sorts desc,cid asc')->select();
		if(!empty($list)){
			foreach($list as $k=>$v){
				$arr[$v['cid']] = $v;
			}
			$lists = $arr;
			$tree = new \Tree();	
			$tree->tree($lists);
			$lists = $tree->getArray();
		}	
		$this->assign('tabs',"分类管理");
		$navi = $this->navigation(2);
		$this->navig = $navi['nav']; 
		$this->lasturl = $navi['lasturl'];
		$this->callback = $this->lasturl;//回调地址		
		$this->assign('empty','<tr><td colspan="3" align="center">没有找到数据</td></tr>');		
		$this->assign("lists", $lists); //数据循环变量				
		$this->display("template/news/cate");		
	}
	
	/*
	*	分类编辑
	*/
	public function cate_edit(){		
		$arr = array();
		$res['sorts'] = 100;
		$pid = 0;	
		$this->model = "News_cate";	
		$Cate = M($this->model);		
		$this->assign('act',"add");//操作方法
		$this->assign('tabs',"新增分类");//功能名称
		$navi = $this->navigation(21);
		if($_GET['act']=="edit"){	
			$res = $Cate->where('cid='.$_GET['cid'])->find();
			$pid = $res['pid'];
			$this->assign('act',"edit");//操作方法
			$this->assign('tabs',"编辑分类");//功能名称
			$navi = $this->navigation(22);	
		}	
		$this->cate = cateFun($this->model,$pid);//分类
		$this->res = $res;
		$this->navig = $navi['nav']; 
		$this->lasturl = $navi['lasturl'];
		$this->callback = $this->lasturl;//回调地址
		$this->display("template/news/cate_edit");	
	}
	
	public function order($i=1){
		$i=$i-1;
		$order = array("id desc","id asc",'sorts desc');	
		return $order[$i];
	}
	
	//设置导航
	public function getNav(){
	   $nav[1] = array(
	   			  "name"=>"文章管理",
				  "url"=>"News/lists/",
	          );		    		  		  			  			  
	   $nav[11] = array(
	   			  "name"=>"新增文章",
	   		  );
	   $nav[12] = array(
	   			  "name"=>"修改文章",
	   		  );
		
	   $nav[2] = array(
	   			  "name"=>"分类管理",
				  "url"=>"News/cate/",
	          );		    		  		  			  			  
	   $nav[21] = array(
	   			  "name"=>"新增分类",
	   		  );
	   $nav[22] = array(
	   			  "name"=>"编辑分类",
	   		  );
			  	  		  			  		  
	   return $nav;		  			  			  		  	  		
   }	

	
}